<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="33%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        size="mini"
        ref="ruleForm"
        label-width="110px"
        class="demo-ruleForm"
      >
        <el-form-item label="系统名称：" prop="systemId">
          <el-select v-model="ruleForm.systemId" placeholder="请选择系统名称" size="small" @change="selectSysChanged">
            <el-option
                v-for="(item,index) in systemList"
                :key="index"
                :label="item.name"
                :value="item.id "
                ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="环境名称：" prop="environmentId">
          <el-select v-model="ruleForm.environmentId" placeholder="请选择环境名称" size="small" @change="selectEnvChanged">
            <el-option
            v-for="(item,index) in  envList"
            :key="index"
            :label="item.name"
            :value="item.id "
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="服务名称：" prop="serviceId">
          <el-select
            v-model="ruleForm.serviceId "
            placeholder="请选择服务名称"
            size="small"
          >
          <el-option
            v-for="(item,index) in serverList"
            :key="index"
            :label="item.name"
            :value="item.id "
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="接口名称：" prop="interFaceName" v-if="type == 2">
          <el-select
            v-model="ruleForm.interFaceName"
            placeholder="请选择接口名称"
            size="small"
          >
            <el-option label="接口名称1" value="1"></el-option>
            <el-option label="接口名称2" value="2"></el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.interFaceName"></el-input> -->
        </el-form-item>
        <el-form-item label="是否启用：" prop="enable">
          <el-select v-model="ruleForm.enable" placeholder="请选择" size="small">
            <el-option label="是" :value="1"></el-option>
            <el-option label="否" :value="0"></el-option>
          </el-select>
        </el-form-item>
<!-- 
        <el-form-item label="任务处理器" prop="jobHandler">
          <el-input v-model="ruleForm.jobHandler"></el-input>
        </el-form-item> -->

        <el-form-item label="调度表达式：" prop="scheduleExpression">
          <el-input v-model="ruleForm.scheduleExpression" size="small"></el-input>
        </el-form-item>

        <el-form-item label="IP：" prop="ip">
          <el-input v-model="ruleForm.ip" size="small"></el-input>
        </el-form-item>
        <el-form-item label="端口：" prop="port">
          <el-input v-model="ruleForm.port" size="small"></el-input>
        </el-form-item>
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="ruleForm.username" size="small"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model="ruleForm.password" size="small"></el-input>
        </el-form-item>
        <el-form-item label="正则表达式：" prop="processRegularExpression">
          <el-input v-model="ruleForm.processRegularExpression" size="small"></el-input>
        </el-form-item>

        <el-form-item label="报警邮箱：" prop="alarmEmail">
          <el-select v-model="ruleForm.alarmEmail " placeholder="请选择报警邮箱" size="small">
            <el-option
              v-for="(item,index) in emailList"
              :key="index"
              :label="item.email"
              :value="item.id"
              ></el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.email"></el-input> -->
        </el-form-item>

        <el-form-item label="报警手机号：" prop="alarmCellPhone">
          <el-select v-model="ruleForm.alarmCellPhone " placeholder="请选择手机号" size="small">
            <el-option
              v-for="(item,index) in phoneList"
              :key="index"
              :label="item.cellPhone"
              :value="item.id"
              ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('ruleForm')"
        >确 定</el-button>
      <el-button size="small" @click="resetForm('ruleForm')">取 消</el-button>
      
    </span>
  </el-dialog>
</template>

<script>
import {addTaskList} from '@/api/serviceSurvive';
import {queryAlarmCellPhoneList,queryAlarmEmailList} from '@/api/serverMonitor'
import {systemFind,environmentData,serviceData} from '@/api/log';
import { getServiceData } from "@/api/configuration/serviceApi";
import { changeParam } from "@/utils/helper";
export default {
  props: ["type", "title"],
  components: {},
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        systemId: "",
        environmentId: "",
        serviceId: "",
        interFaceName: "",
        enable: "",
        jobHandler: "",
        scheduleExpression: "",
        alarmEmail: "",
        alarmCellPhone: "",
        ip: "",
        port: "",
        username: "",
        password: "",
        processRegularExpression: "",
      },
      rules: {
        systemId: [
          { required: true, message: "请输入系统名称", trigger: "change" },
        ],
        environmentId: [
          { required: true, message: "请输入环境名称", trigger: "change" },
        ],
        serviceId: [
          { required: true, message: "请输入服务名称", trigger: "change" },
        ],
        interFaceName: [
          { required: true, message: "请输入接口名称", trigger: "change" },
        ],
        enable: [{ required: true, message: "请选择是否启用", trigger: "change" }],
        alarmEmail: [{ required: true, message: "请输入报警邮箱", trigger: "change" }],
        alarmCellPhone: [{ required: true, message: "请输入报警手机号", trigger: "change" }],
        scheduleExpression: [{ required: true, message: "请输入调度表达式", trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        port: [{ required: true, message: "请输入端口号", trigger: "blur" }],
        ip: [{ required: true, message: "请输入IP", trigger: "blur" }],
      },
      phoneList:[],
      emailList:[],
      serverList:[],
      systemList:[],
      envList:[],
    };
  },
  methods: {
    open(data) {
      if (data != 1) {
        console.log(data,'datadatadata');
        this.ruleForm = { ...data };
      }else{
        console.log(data,'调度表达式');
        delete this.ruleForm.id;
      }
      this.dialogVisible = true;
    },
    //级联关系
    selectSysChanged(){
      const { systemId } = this.ruleForm;
      console.log(this.systemId,'systemIdsystemId');
      this.getenvDataList(systemId);
      this.getserverDataList('',systemId);
      this.ruleForm.environmentId = "";
      this.ruleForm.serviceId = "";
    },
    selectEnvChanged() {
      const { environmentId } = this.ruleForm;
      const { systemId } = this.ruleForm;
      console.log(environmentId,systemId,'environmentId,systemId');
      this.ruleForm.serviceId = "";
      this.getserverDataList(environmentId,systemId);
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs['ruleForm'].resetFields();
      this.ruleForm.systemId= "";
      this.ruleForm.environmentId= "";
      this.ruleForm.serviceId= "";
      this.ruleForm.interFaceName= "";
      this.ruleForm.enable= "";
      this.ruleForm.scheduleExpression= "";
      this.ruleForm.alarmEmail= "";
      this.ruleForm.alarmCellPhone= "";
      this.ruleForm.ip= "";
      this.ruleForm.port= "";
      this.ruleForm.username= "";
      this.ruleForm.password= "";
      this.ruleForm.processRegularExpression= "";
      // this.$refs.ruleForm.resetFields();
     
    },
    close() {
      this.dialogVisible = false;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addTaskList(this.ruleForm).then(res=>{
            if(res.result){
              this.$message.success(res.message);
              this.$refs.ruleForm.resetFields();
              this.dialogVisible = false;
              this.$emit('handleEvent')
            }else{
              this.$message.warning(res.message)
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    getDataList() {
      let params = {
        name: "",
        pageNo: 1,
        pageSize: 99999,
      };
      systemFind(changeParam({ ...params })).then((res) => {
        if (res.success) {
          this.systemList = res.result.list;
        }
      });
    },
    getenvDataList(systemId){
      let envParams = {
        systemId: systemId,
        systemName: "",
        name: "",
        pageNo: 1,
        pageSize: 99999,
      };
      environmentData(changeParam({ ...envParams })).then((res) => {
        if (res.success) {
          this.envList = res.result;
        }
      });
    },
    getserverDataList(environmentId,systemId){
      const params = { environmentId,systemId, pageNo: 1, pageSize: 9999 };
      getServiceData(changeParam({ ...params })).then((res) => {
        if (res.success) {
          this.serverList = res.result.list;
        }
      });
    },
  },
  created() {
    this.getDataList();
    this.getserverDataList();
    this.getenvDataList();
  },
};
</script>
<style lang="scss" scoped>
.demo-ruleForm{
  width: 80%;
  margin: auto;
  ::v-deep .el-input--small .el-input__inner {
        font-size: 14px !important;
    }
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
::v-deep .el-dialog__header {
    padding: 20px 0px 10px;
    border-bottom: 1px solid #DDDDDD;
    margin: 0 20px;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    line-height: 24px;
  }
::v-deep .el-dialog__body {
  padding-top: 15px;
  .el-form-item__label{
    text-align: left !important;
  }
  
}
 .el-form-item.is-required ::v-deep .el-form-item__label:before {
    content: '*';
    color: #F56C6C;
    margin-right: 4px;
  }
  .el-form-item ::v-deep .el-form-item__label:before {
    content: '';
    margin-right: 8px;
  }
.el-select {
  width: 100%;
}
.el-input {
  width: 100%;
}
.el-button--primary{
  background-color: #3C77FF;
  border-color: #3C77FF;
}
.el-button--text{
  color: #3C77FF;
}
.el-button.is-plain:focus, .el-button.is-plain:hover {
  background: #FFF;
  border-color: #3C77FF;
  color: #3C77FF;
}
</style>
